const httpRequest = ({
	url,
	method = 'GET',
	data = {},
	header = {}
}) => {
	const baseUrl = import.meta.env.MODE ==='development' ? 'http://localhost:8002' : 'https://www.baidu.com'
	const token = uni.getStorageSync('token')
	const realHeader = token ? {...header, token} : header
	
	//token:123, {header: {a:'name'}}那么 {...header, token} => {a: 'name', token: 123}
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '请求中...'
		})
		uni.request({
			url: baseUrl + url,
			method,
			data,
			header: realHeader,
			success(res) {
				// {name: '李少斌'}  => {data: {name: '李少斌'}}
				if(res.statusCode===200) {
					resolve(res.data)
				} else {
					uni.showToast({
						title: '服务器异常'
					})
					reject('服务起异常')
				}
				uni.hideLoading()
			},
			fail(err) {
				reject(err)
				uni.showToast({
					title: '服务器异常'
				})
				uni.hideLoading()
			}
		})
	})
	
}
export default httpRequest